package cn.edu.cqu.bysj.wm.gui.component;

import javax.swing.ButtonGroup;
import javax.swing.JPanel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import javax.swing.JCheckBox;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;

import cn.edu.cqu.bysj.wm.def.ConstDef;
import cn.edu.cqu.bysj.wm.gui.ImageButton;
import cn.edu.cqu.bysj.wm.gui.model.AlgorithmConfig;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class JDRightTabContent extends JPanel {

	private JTextField globalParam;
	private JTextField localParam;

	public JTextField getLocalParam() {
		return localParam;
	}

	public void setLocalParam(JTextField localParam) {
		this.localParam = localParam;
	}

	private JCheckBox checkBox;
	private JCheckBox checkBox_1;
	private JCheckBox checkBox_2;
	private JCheckBox checkBox_3;
	private JCheckBox checkBox_4;
	private JCheckBox checkBox_5;
	private JRadioButton localButton;
	private JRadioButton globalButton;
	private ButtonGroup bGroup;
	private AlgorithmConfig configure;

	/**
	 * create key node right tab content
	 * 
	 * @param config1
	 */
	public JDRightTabContent(AlgorithmConfig config1) {
		this.configure = config1;
		globalParam = new JTextField();
		globalParam.setColumns(10);
		globalParam.setText("20");

		localParam = new JTextField();
		localParam.setColumns(10);
		localParam.setEnabled(false);
		localParam.setText("0.5");

		JLabel label_1 = new JLabel("Measures to Choose");

		checkBox = new JCheckBox("Degree Centrality");
		checkBox.setSelected(true);

		checkBox_1 = new JCheckBox("Betweenness Centrality");
		checkBox_1.setSelected(true);

		checkBox_2 = new JCheckBox("Stress Centrality");
		checkBox_2.setSelected(true);

		checkBox_3 = new JCheckBox("Closeness Centrality");
		checkBox_3.setSelected(true);

		checkBox_4 = new JCheckBox("Cluster Coefficient");
		checkBox_4.setSelected(true);

		checkBox_5 = new JCheckBox("PageRank");
		checkBox_5.setSelected(true);

		globalButton = new JRadioButton(ConstDef.GLOBAL_KEY_NODE_STRING);
		globalButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (((JRadioButton) (e.getSource())).isSelected()) {
					globalParam.setEnabled(true);
					localParam.setEnabled(false);
				} else {
					globalParam.setEnabled(false);
					localParam.setEnabled(true);
				}
			}
		});
		globalButton.setActionCommand(ConstDef.GLOBAL_KEY_NODE_STRING);
		globalButton.setSelected(true);
		setLocalButton(new JRadioButton(ConstDef.LOCAL_KEY_NODE_STRING));
		getLocalButton().setActionCommand(ConstDef.LOCAL_KEY_NODE_STRING);
		getLocalButton().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (configure.getAlgorithmType().equals(
						ConstDef.AlgorithmType.SKYLINE)) {
					return;
				}
				if (((JRadioButton) (e.getSource())).isSelected()) {
					globalParam.setEnabled(false);
					localParam.setEnabled(true);
				} else {
					globalParam.setEnabled(true);
					localParam.setEnabled(false);
				}
			}
		});
		bGroup = new ButtonGroup();
		bGroup.add(globalButton);
		bGroup.add(getLocalButton());

		JLabel label_2 = new JLabel("Gloabel or Local");

		JButton button = ImageButton.makeNavigationButton("run.png", "Run",
				"Run", "Run", null, 60, 60);
		// JButton button =new JButton("run");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				doAlgorithm();
			}
		});

		JLabel lblTopk = new JLabel("Top-K");

		JLabel lblPercent = new JLabel("Percent");
		GroupLayout gl_panel = new GroupLayout(this);
		gl_panel.setHorizontalGroup(gl_panel
				.createParallelGroup(Alignment.LEADING)
				.addGroup(
						gl_panel.createSequentialGroup()
								.addContainerGap()
								.addComponent(label_2,
										GroupLayout.DEFAULT_SIZE, 249,
										Short.MAX_VALUE).addGap(127))
				.addGroup(
						gl_panel.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										gl_panel.createParallelGroup(
												Alignment.LEADING)
												.addGroup(
														gl_panel.createSequentialGroup()
																.addComponent(
																		button,
																		GroupLayout.PREFERRED_SIZE,
																		103,
																		GroupLayout.PREFERRED_SIZE)
																.addContainerGap())
												.addGroup(
														gl_panel.createSequentialGroup()
																.addGroup(
																		gl_panel.createParallelGroup(
																				Alignment.LEADING)
																				.addComponent(
																						globalButton,
																						GroupLayout.DEFAULT_SIZE,
																						GroupLayout.DEFAULT_SIZE,
																						Short.MAX_VALUE)
																				.addComponent(
																						localButton,
																						GroupLayout.DEFAULT_SIZE,
																						GroupLayout.DEFAULT_SIZE,
																						Short.MAX_VALUE))
																.addPreferredGap(
																		ComponentPlacement.RELATED)
																.addGroup(
																		gl_panel.createParallelGroup(
																				Alignment.TRAILING,
																				false)
																				.addComponent(
																						lblPercent,
																						GroupLayout.PREFERRED_SIZE,
																						72,
																						GroupLayout.PREFERRED_SIZE)
																				.addComponent(
																						lblTopk,
																						GroupLayout.PREFERRED_SIZE,
																						72,
																						GroupLayout.PREFERRED_SIZE))
																.addPreferredGap(
																		ComponentPlacement.RELATED)
																.addGroup(
																		gl_panel.createParallelGroup(
																				Alignment.LEADING,
																				false)
																				.addComponent(
																						globalParam,
																						GroupLayout.PREFERRED_SIZE,
																						47,
																						GroupLayout.PREFERRED_SIZE)
																				.addComponent(
																						localParam,
																						GroupLayout.PREFERRED_SIZE,
																						47,
																						GroupLayout.PREFERRED_SIZE))
																.addGap(194))))
				.addGroup(
						gl_panel.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										gl_panel.createParallelGroup(
												Alignment.LEADING)
												.addGroup(
														gl_panel.createSequentialGroup()
																.addGroup(
																		gl_panel.createParallelGroup(
																				Alignment.LEADING)
																				.addGroup(
																						gl_panel.createSequentialGroup()
																								.addGroup(
																										gl_panel.createParallelGroup(
																												Alignment.LEADING,
																												false)
																												.addComponent(
																														checkBox,
																														GroupLayout.PREFERRED_SIZE,
																														198,
																														GroupLayout.PREFERRED_SIZE)
																												.addComponent(
																														checkBox_1,
																														GroupLayout.PREFERRED_SIZE,
																														213,
																														GroupLayout.PREFERRED_SIZE))
																								.addGap(125))
																				.addComponent(
																						checkBox_2,
																						GroupLayout.DEFAULT_SIZE,
																						GroupLayout.DEFAULT_SIZE,
																						Short.MAX_VALUE)
																				.addGroup(
																						gl_panel.createSequentialGroup()
																								.addComponent(
																										checkBox_3,
																										GroupLayout.DEFAULT_SIZE,
																										223,
																										Short.MAX_VALUE)
																								.addGap(93))
																				.addGroup(
																						gl_panel.createSequentialGroup()
																								.addComponent(
																										checkBox_4,
																										GroupLayout.DEFAULT_SIZE,
																										GroupLayout.DEFAULT_SIZE,
																										Short.MAX_VALUE)
																								.addGap(135))
																				.addGroup(
																						gl_panel.createSequentialGroup()
																								.addComponent(
																										checkBox_5)
																								.addPreferredGap(
																										ComponentPlacement.RELATED,
																										259,
																										GroupLayout.PREFERRED_SIZE)))
																.addContainerGap())
												.addGroup(
														gl_panel.createSequentialGroup()
																.addComponent(
																		label_1,
																		GroupLayout.PREFERRED_SIZE,
																		223,
																		GroupLayout.PREFERRED_SIZE)
																.addGap(153)))));
		gl_panel.setVerticalGroup(gl_panel
				.createParallelGroup(Alignment.LEADING)
				.addGroup(
						gl_panel.createSequentialGroup()
								.addGap(18)
								.addComponent(label_1,
										GroupLayout.DEFAULT_SIZE, 25,
										Short.MAX_VALUE)
								.addPreferredGap(ComponentPlacement.RELATED)
								.addComponent(checkBox,
										GroupLayout.DEFAULT_SIZE, 34,
										Short.MAX_VALUE)
								.addPreferredGap(ComponentPlacement.UNRELATED)
								.addComponent(checkBox_1,
										GroupLayout.DEFAULT_SIZE, 34,
										Short.MAX_VALUE)
								.addPreferredGap(ComponentPlacement.UNRELATED)
								.addComponent(checkBox_2,
										GroupLayout.DEFAULT_SIZE, 34,
										Short.MAX_VALUE)
								.addPreferredGap(ComponentPlacement.UNRELATED)
								.addComponent(checkBox_3,
										GroupLayout.DEFAULT_SIZE, 34,
										Short.MAX_VALUE)
								.addPreferredGap(ComponentPlacement.UNRELATED)
								.addComponent(checkBox_4,
										GroupLayout.DEFAULT_SIZE, 34,
										Short.MAX_VALUE)
								.addPreferredGap(ComponentPlacement.UNRELATED)
								.addComponent(checkBox_5,
										GroupLayout.DEFAULT_SIZE, 33,
										Short.MAX_VALUE)
								.addGap(33)
								.addComponent(label_2,
										GroupLayout.DEFAULT_SIZE, 22,
										Short.MAX_VALUE)
								.addGap(18)
								.addGroup(
										gl_panel.createParallelGroup(
												Alignment.BASELINE)
												.addComponent(
														globalButton,
														GroupLayout.DEFAULT_SIZE,
														GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE)
												.addComponent(
														lblTopk,
														GroupLayout.DEFAULT_SIZE,
														GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE)
												.addComponent(
														globalParam,
														GroupLayout.PREFERRED_SIZE,
														24,
														GroupLayout.PREFERRED_SIZE))
								.addGap(9)
								.addGroup(
										gl_panel.createParallelGroup(
												Alignment.BASELINE)
												.addComponent(
														localButton,
														GroupLayout.DEFAULT_SIZE,
														GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE)
												.addComponent(
														lblPercent,
														GroupLayout.DEFAULT_SIZE,
														GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE)
												.addComponent(
														localParam,
														GroupLayout.PREFERRED_SIZE,
														GroupLayout.DEFAULT_SIZE,
														GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(ComponentPlacement.UNRELATED)
								.addComponent(button,
										GroupLayout.PREFERRED_SIZE, 78,
										GroupLayout.PREFERRED_SIZE).addGap(55)));
		this.setLayout(gl_panel);
	}

	public JTextField getGlobalParam() {
		return globalParam;
	}

	public void setGlobalParam(JTextField globalParam) {
		this.globalParam = globalParam;
	}

	public JRadioButton getGlobalButton() {
		return globalButton;
	}

	public void setGlobalButton(JRadioButton globalButton) {
		this.globalButton = globalButton;
	}

	public ButtonGroup getbGroup() {
		return bGroup;
	}

	public void setbGroup(ButtonGroup bGroup) {
		this.bGroup = bGroup;
	}

	protected void doAlgorithm() {

		((JDTotalTabPane) (this.getParent().getParent())).doAlgorithm();
	}

	public int[] getIndex() {
		int[] index = new int[] { 0, 0, 0, 0, 0, 0 };
		if (checkBox.isSelected()) {
			index[0] = 1;
		}
		if (checkBox_1.isSelected()) {
			index[1] = 1;
		}
		if (checkBox_2.isSelected()) {
			index[2] = 1;
		}
		if (checkBox_3.isSelected()) {
			index[3] = 1;
		}
		if (checkBox_4.isSelected()) {
			index[4] = 1;
		}
		if (checkBox_5.isSelected()) {
			index[5] = 1;
		}
		return index;
	}

	public JRadioButton getLocalButton() {
		return localButton;
	}

	public void setLocalButton(JRadioButton localButton) {
		this.localButton = localButton;
	}
}
